//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Policy Injection Application Block
//===============================================================================

using System;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
using EntLibContrib.PolicyInjection.Configuration;

namespace EntLibContrib.PolicyInjection.MatchingRules
{
	/// <summary>
	/// And Matching Rule
	/// </summary>
	[ConfigurationElementType(typeof(AndMatchingRuleData))]
	public class AndMatchingRule : IMatchingRule
	{
		#region Fields
		private List<IMatchingRule> rules;
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="AndMatchingRule"/> class.
		/// </summary>
		/// <param name="rules">The rules.</param>
		public AndMatchingRule(IEnumerable<IMatchingRule> rules)
		{
			if (rules == null) 
				throw new ArgumentNullException("rules");

			this.rules = new List<IMatchingRule>(rules);
		}
		#endregion

		#region IMatchingRule Members
		/// <summary>
		/// Matches the specified member.
		/// </summary>
		/// <param name="member">The member.</param>
		/// <returns>
		/// true if the rule applies, false if it doesn't.
		/// </returns>
		public bool Matches(MethodBase member)
		{
			if (rules.Count == 0) return true;
			foreach (IMatchingRule rule in rules)
			{
				if (!rule.Matches(member))
				{
					return false;
				}
			}
			return true;
		}
		#endregion
	}
}
